﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Data.SqlTypes;

namespace WindowsFormsApplication1
{
    public partial class User : Form
    {
        public User()
        {
            InitializeComponent();
        }

        private void cmbRole_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                String val = cmbRole.SelectedItem.ToString();
                if (val.Equals("ADMIN"))
                {
                    String getMax = "SELECT MAX(UserID) FROM loggin WHERE UserID LIKE 'A%' ";
                    String id = "";
                    SqlDataReader mxdr = DbConnet.ReadTable(getMax);
                    int count = 0;

                    if (mxdr.Read())
                    {
                        id = mxdr[0].ToString();
                    }
                    mxdr.Close();

                    id = id.Substring(id.IndexOf("A") + 1);
                    try
                    {
                        count = int.Parse(id);
                    }
                    catch (Exception excon)
                    {
                    }

                    txtUid.Text = "A00" + (count + 1);
                }
                else
                {
                    if (val.Equals("USER"))
                    {
                        String getMax = "SELECT MAX(UserID) FROM loggin WHERE UserID LIKE 'U%' ";
                        String id = "";
                        SqlDataReader mxdr = DbConnet.ReadTable(getMax);
                        int count = 0;

                        if (mxdr.Read())
                        {
                            id = mxdr[0].ToString();
                        }
                        mxdr.Close();

                        id = id.Substring(id.IndexOf("U") + 1);
                        try
                        {
                            count = int.Parse(id);
                        }
                        catch (Exception excon)
                        {
                        }

                        txtUid.Text = "U00" + (count + 1);
                    }
                }
            }
            catch (Exception exuc)
            {
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (txtUid.Text.Trim().Length <= 0)
            {
                MessageBox.Show(new Form() { TopMost = true }, "Please Select User Role to Genarate User Id ", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtUid.Select();
                txtUid.Focus();
            }
            else
            {
                if (txtUname.Text.Trim().Length <= 0)
                {
                    MessageBox.Show(new Form() { TopMost = true }, "Please Enter User Name ", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    txtUname.Select();
                    txtUname.Focus();
                }
                else
                {
                    if (txtName.Text.Trim().Length <= 0)
                    {
                        MessageBox.Show(new Form() { TopMost = true }, "Please Enter Name ", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        txtName.Select();
                        txtName.Focus();
                    }
                    else
                    {
                        if (txtpass.Text.Trim().Length <= 0)
                        {
                            MessageBox.Show(new Form() { TopMost = true }, "Please Enter Password ", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            txtpass.Select();
                            txtpass.Focus();
                        }
                        else
                        {
                            try
                            {

                                String role = "";
                                String val = cmbRole.SelectedItem.ToString();

                                switch (val)
                                {
                                    case"ADMIN":
                                        role = "1";
                                        break;
                                    case "USER":
                                        role = "2";
                                        break;
                                }

                                String updUser = "UPDATE loggin SET password = '" + txtpass.Text.Trim() + "',updatedby = '" + new Midval().getUser() + "',updateddate = '"+DateTime.Now+"' WHERE UserID = '" + txtUid.Text.Trim() + "'";

                                String insUser = "INSERT INTO loggin (UserID,UserName,Role,password,name,createdby,crateddate) VALUES ('"+txtUid.Text.Trim()+"','"+txtUname.Text.Trim()+"', "+ 
                                                 " '"+role+"' , '"+txtpass.Text.Trim()+"','"+txtName.Text.Trim()+"','"+new Midval().getUser()+"','"+DateTime.Now+"') ";


                                Boolean upd = DbConnet.AddEditDel(updUser);
                                Boolean ins = false;

                                if(!upd)
                                    ins = DbConnet.AddEditDel(insUser);

                                if (upd || ins)
                                {
                                    MessageBox.Show(new Form() { TopMost = true }, "User Created/Updated Successfully", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                    cmbRole.SelectedIndex = -1;
                                    txtName.Clear();
                                    txtpass.Clear();
                                    txtUid.Clear();
                                    txtUname.Clear();
                                }

                            }
                            catch (Exception exins)
                            {
                            }
                        }
                    }
                }
            }
        }

        private void User_Load(object sender, EventArgs e)
        {
            DbConnet.connect(new Log().getConstr());
        }

        private void txtUid_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                try
                {
                    String getUser = "SELECT UserID,UserName,Role,password,name FROM loggin WHERE UserID = '" + txtUid.Text.Trim() + "' ";

                    SqlDataReader getd = DbConnet.ReadTable(getUser);

                    while (getd.Read())
                    {
                        txtUid.Text = getd["UserID"].ToString();
                        txtUname.Text = getd["UserName"].ToString();
                        txtName.Text = getd["name"].ToString();
                        String role = getd["Role"].ToString();

                        switch (role)
                        {
                            case "1":
                                cmbRole.SelectedIndex = 0;
                                break;
                            case "2":
                                cmbRole.SelectedIndex = 1;
                                break;
                            default:
                                cmbRole.SelectedIndex = -1;
                                break;
                        }
                    }
                    getd.Close();

                }
                catch (Exception exget)
                {

                }
            }
        }

        private void btncls_Click(object sender, EventArgs e)
        {
            cmbRole.SelectedIndex = -1;
            txtName.Clear();
            txtpass.Clear();
            txtUid.Clear();
            txtUname.Clear();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Hide();
        }
    }
}
